var api = require('./api.js');
//工具函数库
function formatDate(value) {
    let date = new Date(value*1000);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;
    return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
  }
function test1(){
    console.log('test1')
}  
function formatObjDate(obj){
    return new Promise((resolve, reject)=>{
        for(var i=0;i<obj.length;i++){//用for循环把所有的时间戳都转换程时间格式，这里调用的是小程序官方demo中带的方法，
            obj[i]["atime"] = formatDate(obj[i]["atime"])
            obj[i]["time"] = formatDate(obj[i]["time"])
        }
        resolve(obj);
    })
}  
function formatOneDate(obj){
    return new Promise((resolve, reject)=>{
        obj["time"] = formatDate(obj["time"])
        resolve(obj);
    })
} 
function request(url, method, data, resbook){
    return new Promise((resolve, reject)=>{
        uni.showLoading({
            title: '加载中...'
        });
        uni.request({
            url: url,
            data: data || {},
            method: method || 'GET',
			// header:{
			// 	"Content-Type": (method=='GET'?"application/json": 
			// 				"application/json")
			// },
            success: function(res) {
				console.log(333,data,res)
                uni.hideLoading();
                if(res.statusCode==200){
					if(resbook){
						resolve(res.data);
					}else{
						resolve(res.data.data);
					}
                }else{
                    var title = res.errMsg
                    showToast(title);  //提示信息
                    reject();
                } 
            }
        });
    })
}
/**
 * 调用微信登录
 */
function login () {
    return new Promise(function (resolve, reject) {
        uni.login({
            provider: 'weixin',
            success: function (res) {
                console.log(111,res);
                if (res.code) {
                // 登录远程服务器
                // console.log('微信登陆成功', res)
                resolve(res);
                } else {
                reject(res);
                }
            },
            fail: function (err) {
                reject(err);
            }
        });
    });
}
/**
 * 调用微信登录
 */
function loginByWeixin (formData) {
    let code = null;
    return new Promise(function (resolve, reject) {
        return login().then((res) => {
            code = res.code;
            // console.log(1231,res)
            return uni.getStorageSync('userInfo');
        }).then((fdata) => {
            // console.log(198,fdata)
            formData = Object.assign(formData,fdata);
            // 把用户信息传给后台，存入数据库，并计算一个token给前台存起来
            request(api.getOpenId,'POST', {
                code: code,
                userInfo: formData
            },123).then(res => {
                resolve(res);
                
            }).catch((err) => {
                reject(err);
            });
        }).catch((err) => {
        reject(err);
        })
    });
}
//toast提示信息
function showToast(title, icon='none'){
    uni.showToast({
        title,
        icon
    });
}

function get(url, data, resbook){
    return request(url, 'GET', data, resbook);
}

function post(url, data, resbook){
    return request(url, 'POST', data, resbook);
}

module.exports = {
    formatDate,
    formatObjDate,
    formatOneDate,
    get,
    post,
    loginByWeixin
}